// pages/create-room/create-room.js
const app = getApp();

Page({
  data: {
    // 游戏类型选项
    gameTypes: [
      { id: 'mahjong', name: '麻将', icon: '../../assets/icons/mahjong.png' },
      { id: 'poker', name: '扑克', icon: '../../assets/icons/poker.png' },
      { id: 'doudizhu', name: '斗地主', icon: '../../assets/icons/doudizhu.png' },
      { id: 'other', name: '其他', icon: '../../assets/icons/other.png' }
    ],
    
    // 初始积分选项
    pointOptions: [
      { label: '1000', value: 1000 },
      { label: '5000', value: 5000 },
      { label: '10000', value: 10000 },
      { label: '自定义', value: -1 }
    ],
    
    // 当前选中的值
    selectedType: 'mahjong',
    roomName: '',
    playerCount: 4,
    selectedPoints: 1000,
    gameDuration: 60,
    allowSpectators: false,
    preventJoin: false,
    autoDissolve: false,
    
    // 按钮状态控制
    canDecreasePlayers: false,
    canIncreasePlayers: true,
    canDecreaseDuration: false,
    canIncreaseDuration: true
  },
  
  onLoad: function() {
    // 设置默认房间名称为用户昵称的房间
    if (app.globalData.userInfo && app.globalData.userInfo.nickname) {
      this.setData({
        roomName: app.globalData.userInfo.nickname + '的房间'
      });
    } else {
      this.setData({
        roomName: '新房间'
      });
    }
    
    // 检查按钮状态
    this.checkButtonStatus();
  },
  
  // 选择游戏类型
  selectGameType: function(e) {
    const gameType = e.currentTarget.dataset.type;
    
    this.setData({ selectedType: gameType });
    
    // 根据游戏类型调整默认玩家数
    if (gameType === 'doudizhu') {
      this.setData({ playerCount: 3 });
    } else if (gameType === 'poker') {
      this.setData({ playerCount: 2 });
    } else {
      this.setData({ playerCount: 4 });
    }
    
    // 重新检查按钮状态
    this.checkButtonStatus();
  },
  
  // 房间名称输入
  onRoomNameInput: function(e) {
    this.setData({ roomName: e.detail.value });
  },
  
  // 减少玩家数
  decreasePlayers: function() {
    if (this.data.playerCount > 2) {
      this.setData({ playerCount: this.data.playerCount - 1 });
      this.checkButtonStatus();
    }
  },
  
  // 增加玩家数
  increasePlayers: function() {
    if (this.data.playerCount < 8) {
      this.setData({ playerCount: this.data.playerCount + 1 });
      this.checkButtonStatus();
    }
  },
  
  // 选择初始积分
  selectPoints: function(e) {
    const points = e.currentTarget.dataset.value;
    
    if (points === -1) {
      // 自定义积分
      wx.showModal({
        title: '自定义积分',
        content: '请输入初始积分',
        editable: true,
        placeholderText: '请输入数字',
        success: res => {
          if (res.confirm && res.content) {
            const customPoints = parseInt(res.content);
            if (!isNaN(customPoints) && customPoints > 0) {
              this.setData({ selectedPoints: customPoints });
            } else {
              wx.showToast({ title: '请输入有效的数字', icon: 'none' });
            }
          }
        }
      });
    } else {
      this.setData({ selectedPoints: points });
    }
  },
  
  // 减少游戏时长
  decreaseDuration: function() {
    if (this.data.gameDuration > 30) {
      this.setData({ gameDuration: this.data.gameDuration - 30 });
      this.checkButtonStatus();
    }
  },
  
  // 增加游戏时长
  increaseDuration: function() {
    if (this.data.gameDuration < 240) {
      this.setData({ gameDuration: this.data.gameDuration + 30 });
      this.checkButtonStatus();
    }
  },
  
  // 切换观战设置
  toggleSpectators: function(e) {
    this.setData({ allowSpectators: e.detail.value });
  },
  
  // 切换禁止中途加入设置
  togglePreventJoin: function(e) {
    this.setData({ preventJoin: e.detail.value });
  },
  
  // 切换自动解散设置
  toggleAutoDissolve: function(e) {
    this.setData({ autoDissolve: e.detail.value });
  },
  
  // 检查按钮状态
  checkButtonStatus: function() {
    const { playerCount, gameDuration } = this.data;
    
    this.setData({
      canDecreasePlayers: playerCount > 2,
      canIncreasePlayers: playerCount < 8,
      canDecreaseDuration: gameDuration > 30,
      canIncreaseDuration: gameDuration < 240
    });
  },
  
  // 创建房间
  createRoom: function() {
    // 验证输入
    if (!this.data.roomName.trim()) {
      wx.showToast({ title: '请输入房间名称', icon: 'none' });
      return;
    }
    
    wx.showLoading({ title: '创建房间中...' });
    
    // 调用创建房间API
    app.request('/room/create', {
      method: 'POST',
      data: {
        name: this.data.roomName,
        game_type: this.data.selectedType,
        max_players: this.data.playerCount,
        initial_points: this.data.selectedPoints,
        duration: this.data.gameDuration,
        allow_spectators: this.data.allowSpectators,
        prevent_join: this.data.preventJoin,
        auto_dissolve: this.data.autoDissolve
      }
    }).then(res => {
      wx.hideLoading();
      
      wx.showToast({
        title: '房间创建成功',
        icon: 'success'
      });
      
      // 延迟跳转到房间详情页
      setTimeout(() => {
        wx.navigateTo({
          url: `/pages/room-detail/room-detail?id=${res.room_id}&code=${res.room_code}`
        });
      }, 1500);
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err.message || '房间创建失败',
        icon: 'none'
      });
    });
  },
  
  // 返回上一页
  navigateBack: function() {
    wx.navigateBack();
  }
});